package com.wh.productMag.controller;

import com.wh.basic.vo.AjaxResult;
import com.wh.productMag.service.IProductClassifyService;
import com.wh.productMag.pojo.ProductClassify;
import com.wh.productMag.dto.ProductClassifyQueryDto;
import com.wh.system.annotation.PreAuthorize;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/productClassify")
public class ProductClassifyController {

    @Autowired
    public IProductClassifyService productClassifyService;

    /**
     * 接口：添加或修改
     * @param productClassify 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PreAuthorize(sn = "productClassify/addOrUpdate",name = "添加或修改产品分类列表")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductClassify productClassify) {
        if ( productClassify.getId() != null){
            System.err.println(1111);
            
            productClassifyService.updateById(productClassify);
        }else{
            productClassifyService.save(productClassify);
        }
        return new AjaxResult();
    }
    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */

    @PreAuthorize(sn = "productClassify/del",name = "删除单个产品分类列表")
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        productClassifyService.removeById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PreAuthorize(sn = "productClassify/patchDel",name = "批量删除产品分类列表")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        productClassifyService.removeByIds(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @PreAuthorize(sn = "productClassify/findOne",name = "根据id查询单个产品分类")
    @GetMapping("/{id}")
    public ProductClassify findOne(@PathVariable("id") Long id) {
        return productClassifyService.getById(id);
    }
    /**
    * 接口：查询所有
    * @return
    */

    @GetMapping
    public List<ProductClassify> findAll() {
        return productClassifyService.list();
    }
    /**
     * 接口：分页查询或高级查询
     * @param productClassifyQueryDto 查询对象
     * @return IPage<ProductClassify> 分页对象
     */
    @PreAuthorize(sn = "productClassify/findByPage",name = "分页查询产品分类列表")
    @PostMapping
    public IPage<ProductClassify> findByPage(@RequestBody ProductClassifyQueryDto productClassifyQueryDto) {
        return productClassifyService.findByPage(productClassifyQueryDto);
    }
    /**
     * 产品分类树 接口
     */
    @GetMapping("/prodTree")
    public List<ProductClassify> getTree(){
        return productClassifyService.prodTree();
    }

}
